package com.mahesh;

import java.sql.*;
import java.util.ArrayList;

import javax.servlet.ServletContext;

public class DBConnection implements ConnectionProvider {
	private static Connection con = null;
	private PreparedStatement ps;
	private ResultSet rs = null;
	private Statement st = null;
	

	public static Connection getCon() throws Exception {
		if (con == null) {
			Class.forName(DRIVER);
			System.out.println("Register");
			con = DriverManager.getConnection(CONN_URL, USERNAME, PASSWORD);
			System.out.println("DriveManager Connection created");
			return con;
		} else {
			System.out.println(" Existing con is returned");
			return con;
		}
	}

	public boolean insert(String fullname, String email, Long mobno, String pwd) {
		
		try {
			con = getCon();
			ps = con.prepareStatement("insert into userdetails values(?,?,?,?)");
			ps.setString(1, fullname);
			ps.setString(2, email);
			ps.setLong(3, mobno);
			ps.setString(4, pwd);
			ps.executeUpdate();
			
			return true;
			
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return false;

	}

	public ResultSet select(String sql) {
		System.out.println("Conn Ok");
		try {
			con = getCon();
			st = con.createStatement();
			System.out.println("Statement Ok");
			rs = st.executeQuery(sql);
			System.out.println("rs execute Ok");
			
		} catch (SQLException e) {
			e.printStackTrace();
		} catch (Exception e) {
		
			e.printStackTrace();
		}
		
		return rs;
		

	}
}